home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / general.programming / comp.lang.c_12233_000018.msg < prev    next >
Encoding:
Text File  |  1994-11-27  |  2.1 KB  |  52 lines

  1. Path: dd.chalmers.se!news.chalmers.se!sunic!trane.uninett.no!eunet.no!EU.net!howland.reston.ans.net!sol.ctr.columbia.edu!news.kei.com!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!tada
  2. From: tada@ATHENA.MIT.EDU (Michael J Zehr)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variable Length Structures ?
  5. Date: 22 Mar 1994 15:36:27 GMT
  6. Organization: Massachusetts Institute of Technology
  7. Lines: 40
  8. Message-ID: <2mn39r$ajs@senator-bedfellow.MIT.EDU>
  9. References: <Cn0qAy.7KB@cbfsb.cb.att.com> <ua302aa.764269592@sun2>
  10. NNTP-Posting-Host: alfredo.mit.edu
  11.  
  12. In article <ua302aa.764269592@sun2> ua302aa@sun2.LRZ-Muenchen.DE (Kurt Watzka) writes:
  13. >ddewar@cbnewsb.cb.att.com (derek.a.dewar) writes:
  14. >>Does anyone out there in C-land know if it's at all possible to have 
  15. >>a variable length structures in C ? Something like the following :
  16. >Try something like this:
  17. >   struct blah {
  18. >      /* fixed fields */
  19. >      int num_fields;
  20. >      int var[ 1 ];
  21. >   }
  22. >
  23. >Then allocate memory like this 
  24. >   struct blah *p = (struct blah *)
  25. >      malloc( sizeof( struct blah ) +
  26. >              (num_fields - 1 ) * sizeof( int ) );
  27. >You can't successfully use sizeof on such a "variable length
  28. >structure". [or assignment]
  29.  
  30.  
  31.  
  32. The C standards committee recently ruled that this is not strictly
  33. conforming and not guaranteed to work.  While it will work on most
  34. platforms, one should be aware of this when using it.  
  35.  
  36. Naturally a C vendor is free to provide this as a language extension,
  37. but if they don't *say* they provide it, then they don't have to.  (No,
  38. I'm not too happy about the situation either.)
  39.  
  40. [The reasoning behind the restriction has to do with compilers that
  41. might do bounds checking based on the declared size of the array.  One
  42. can also imagine segmented architectures for which the size of the
  43. struct is just below the size of a segment, but the extra space makes it
  44. larger than a segment, yet accesses to the array are generated by the
  45. compiler assuming that the array is in the same segment as the rest of
  46. the structure.  I'm not claiming there *are* such architectures, only
  47. that there could be.]
  48.  
  49. *sigh*
  50.  
  51. -michael j zehr
  52.